package com.hly.musicbackground.model.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hly.musicbackground.Enum.UserLogEnum;
import com.hly.musicbackground.Utils.DateUtils;
import com.hly.musicbackground.Utils.DozerUtils;
import com.hly.musicbackground.Utils.RedisUtil;
import com.hly.musicbackground.domain.UserLog;
import com.hly.musicbackground.model.admin.VO.AdminIndexDataVO;
import com.hly.musicbackground.model.admin.service.AdminHomeService;
import com.hly.musicbackground.model.user.service.UserLogService;
import com.hly.musicbackground.model.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AdminHomeServiceImpl implements AdminHomeService {

    private final UserLogService userLogService;
    private final RedisUtil redisUtil;
    private final DozerUtils dozerUtils;
    private final UserService userService;

    @Override
    public AdminIndexDataVO getIndexData() {
        return createIndexData();
    }

    public AdminIndexDataVO createIndexData() {

        AdminIndexDataVO adminIndexDataVO = new AdminIndexDataVO();

        List<Map<String, Object>> maps = new ArrayList<>();
        adminIndexDataVO.setListNumber(maps);

        // 查询昨日新增用户人数
        HashMap<String, Object> addUserNumber = new HashMap<>();
        addUserNumber.put("title", "新增用户人数");
        addUserNumber.put("count", userLogService
                .count(Wrappers.<UserLog>lambdaQuery()
                        .lt(UserLog::getCreateTime, DateUtils.ZeroTime(0, 0, 0))
                        .gt(UserLog::getCreateTime, DateUtils.ZeroTime(0, 0, 1))
                        .eq(UserLog::getUserLogType, UserLogEnum.REGISTER.getValue())));
        maps.add(addUserNumber);

        // 查询昨日活跃人数
        HashMap<String, Object> activeUserNumber = new HashMap<>();
        activeUserNumber.put("title", "昨日活跃人数");
        activeUserNumber.put("count", userLogService.list(Wrappers.<UserLog>lambdaQuery()
                .lt(UserLog::getCreateTime, DateUtils.ZeroTime(0, 0, 0))
                .gt(UserLog::getCreateTime, DateUtils.ZeroTime(0, 0, 1))
                .groupBy(UserLog::getUserId)).size());
        maps.add(activeUserNumber);

        // 查询昨日阅读人数
        HashMap<String, Object> readFictionNumber = new HashMap<>();
        readFictionNumber.put("title", "昨日阅读人数");
        readFictionNumber.put("count", userLogService.list(Wrappers.<UserLog>lambdaQuery()
                .lt(UserLog::getCreateTime, DateUtils.ZeroTime(0, 0, 0))
                .gt(UserLog::getCreateTime, DateUtils.ZeroTime(0, 0, 1))
                .and(n -> {
                    n.eq(UserLog::getUserLogType, UserLogEnum.GET_FICTION_DETAIL);
                    n.or();
                    n.eq(UserLog::getUserLogType, UserLogEnum.READ_FICTION_CHAPTER);
                }).groupBy(UserLog::getUserId)).size());
        maps.add(readFictionNumber);

        // 查询昨日听歌人数
        HashMap<String, Object> playMusicNumber = new HashMap<>();
        playMusicNumber.put("title", "昨日听歌人数");
        playMusicNumber.put("count", userLogService.list(Wrappers.<UserLog>lambdaQuery()
                .lt(UserLog::getCreateTime, DateUtils.ZeroTime(0, 0, 0))
                .gt(UserLog::getCreateTime, DateUtils.ZeroTime(0, 0, 1))
                .and(n -> {
                    n.eq(UserLog::getUserLogType, UserLogEnum.PLAY_MP3);
                    n.or();
                    n.eq(UserLog::getUserLogType, UserLogEnum.PLAY_MP4);
                }).groupBy(UserLog::getUserId)).size());
        maps.add(playMusicNumber);


        // 近期日志记录【取10条】
        adminIndexDataVO.setRecentLogList(userLogService.getRecentLogList(1, 10, 0));

        // 查询昨日个人活跃度【取10条】
        adminIndexDataVO.setActiveUserList(userLogService.getActiveUserList(1, 0, 10));

        // 查询近一个月个人活跃度【取10条】
        adminIndexDataVO.setActiveUserMonthList(userLogService.getActiveUserList(0, 1, 10));

        // 近30天每日活跃度曲线图
        adminIndexDataVO.setActiveMonth(userLogService.getDateActive(30, false));

        // 近一年每日活跃度曲线图
        adminIndexDataVO.setActiveYear(userLogService.getDateActive(12, true));

        return adminIndexDataVO;
    }
}
